/**
 * 
 */
package org.ycsoft.metroid;

import java.io.File;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.InputStream;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.MenuItem;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;

/**
 * @author lookouster
 *
 */
public class MetroidMain extends Activity {
	public TextView mainTitle, footer;
	public ImageButton defaultLoad, mapsLoad, prefsLoad, downLoad,abt;
	public String mapFile, lang, mapName;
	private static final int  DEF_ID=1;
	private static final int PREFS_ID=2;
	private static final int MAPS_ID=3;
	private static final int DOWN_ID=4;
	private static final int EXIT_ID=5;
	private static final int ABOUT_ID=6;
	
	@Override
	public void onCreate(Bundle icicle)
	{         
		super.onCreate(icicle);
		setContentView(R.layout.metroid_main);
		//mainTitle=(TextView)findViewById(R.id.title_prefs);
		defaultLoad=(ImageButton)findViewById(R.id.load_default);
		mapsLoad=(ImageButton)findViewById(R.id.load_maps);
		prefsLoad=(ImageButton)findViewById(R.id.load_prefs);
		downLoad=(ImageButton)findViewById(R.id.load_down);
		abt=(ImageButton)findViewById(R.id.load_abt);
		footer=(TextView)findViewById(R.id.copy);
		defaultLoad.setOnClickListener(defListener);
		mapsLoad.setOnClickListener(mapsListener);
		prefsLoad.setOnClickListener(prefsListener);
		downLoad.setOnClickListener(downListener);
		abt.setOnClickListener(abtListener);
		footer.setTag(R.string.welcome);
		defaultLoad.setOnFocusChangeListener(new OnFocusChangeListener(){
			@Override
			public void onFocusChange(View v, boolean hasFocus) {
				// TODO Auto-generated method stub
				if(v.isFocused())
					footer.setText(R.string.def_load);
			}
			
		});
		
		mapsLoad.setOnFocusChangeListener(new OnFocusChangeListener(){
			
			public void onFocusChange(View v, boolean b)
			{
				if(v.isFocused())
					footer.setText(R.string.maps_load);
			}
		});
		
		prefsLoad.setOnFocusChangeListener(new OnFocusChangeListener(){
					
			public void onFocusChange(View v, boolean b)
			{
				if(v.isFocused())
					footer.setText(R.string.prefs_load);
			}
		});
		
		downLoad.setOnFocusChangeListener(new OnFocusChangeListener(){
				
				public void onFocusChange(View v, boolean b)
				{
					if(v.isFocused())
						footer.setText(R.string.down_load);
				}
			});
		
		copyMaps();
	}
	
	private void copyMaps() {
		SharedPreferences settings = getSharedPreferences(
	    		  Commons.PREFS_NAME, 0);
		String rep = settings.getString(Commons.PREFS_FOLDER, Commons.PREFS_DEF_FOLDER);
		File dir = new File(rep);
		// String[] files=dir.list();
		File[] files = dir.listFiles(filtre);
		if (files == null || files.length == 0) {
			//ok. on va ajouter les cartes
			copyMapFile(R.raw.atlanta,rep,"atlanta.mtd");
			copyMapFile(R.raw.milan,rep,"milan.mtd");
			copyMapFile(R.raw.sanfrancisco,rep,"sanfrancisco.mtd");
			copyMapFile(R.raw.paris,rep,"paris.mtd");
			copyMapFile(R.raw.washington,rep,"washington.mtd");
		}
		
	}
	
	private void copyMapFile(int resId,String rep,String filename){
		try{
			InputStream is = getResources().openRawResource(resId);
			FileOutputStream fos = new FileOutputStream(rep+filename);
			byte[] buffer = new byte[8*1024];
			int n=-1;
			while( (n=is.read(buffer)) != -1){
				fos.write(buffer,0,n);
				fos.flush();
			}
			fos.close();
		}catch(Exception e){
			//ignore
		}
	}
	
	/*
	 * Filter for metroid files (.mtd extensions)
	 */
	FilenameFilter filtre = new FilenameFilter() {

		public boolean accept(File dir, String filename) {
			return filename.endsWith(".mtd");
		}
	};

	public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        menu.add(0, DEF_ID,0, R.string.def_load);
        menu.add(0, PREFS_ID,0, R.string.prefs_load);
        menu.add(0, MAPS_ID, 0,R.string.maps_load);
        menu.add(0, DOWN_ID, 0,R.string.down_load);
        menu.add(0, EXIT_ID, 0,R.string.close);
        menu.add(0, ABOUT_ID, 0,R.string.about);
        return true;
    }
	
	
	public boolean onMenuItemSelected(int featureId, MenuItem item) {
        switch(item.getItemId()) {
        case DEF_ID:
        	startMetroidCentral();
            return true;
        case PREFS_ID:
        	startMetroidPrefs();
            return true;
        case MAPS_ID:
        	startMetroidMap();
        	return true;
        case DOWN_ID:
        	startMetroidRepo();
        	return true;
        case ABOUT_ID:
        	launchAPropos();
        	return true;
        case EXIT_ID:
        	finish();        	
        }
       
        return super.onMenuItemSelected(featureId, item);
    }

	
	
	
		private OnClickListener defListener = new OnClickListener(){
			public void onClick(View v){
				startMetroidCentral();
			}
		};
		
		private OnClickListener mapsListener = new OnClickListener(){
			public void onClick(View v){
				startMetroidMap();
			}
		};
		
		private OnClickListener prefsListener = new OnClickListener(){
			public void onClick(View v){
				startMetroidPrefs();
			}
		};
		
		private OnClickListener downListener = new OnClickListener(){
			public void onClick(View v){
				startMetroidRepo();
			}
		};
		
		private OnClickListener abtListener=new OnClickListener(){

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				launchAPropos();
			}
		};
		
		public void launchAPropos()
		{
			AProposMetroid propos=new AProposMetroid(this);
			propos.show();
		}
		
		private void startMetroidCentral()
		{
			SharedPreferences settings = getSharedPreferences(
					Commons.PREFS_NAME, 0);
			mapFile=settings.getString(Commons.PREFS_MAPFILE,
			Commons.PREFS_DEF_MAPFILE);
			/*lang=settings.getString(Commons.PREFS_LANG,
					Commons.PREFS_DEF_LANG);*/
			mapName=settings.getString(Commons.PREFS_CITY, Commons.PREFS_DEF_CITY);
			Intent i = new Intent(this, MetroidCentral.class);
			i.putExtra(Commons.KEY_MAPNAME, mapName);
			i.putExtra(Commons.KEY_MAPFILE, mapFile);
			//i.putExtra(Commons.PREFS_LANG, lang);
			startActivity(i);
			//showAlert("Error",R.drawable.icon,mapFile+"-"+lang+"-"+mapName, "ok", false);
		}
		
		private void startMetroidMap()
		{
			Intent i = new Intent(this, MetroidMap.class);
			startActivity(i);
		}
		
		private void startMetroidPrefs()
		{
			Intent i = new Intent(this, MetroidPrefs.class);
			startActivity(i);
		}
		
		private void startMetroidRepo()
		{
			Intent i = new Intent(this, RepoMetroid.class);
			startActivity(i);
		}
	
}
	